<?php

/**
 * This is the model class for table "{{educators}}".
 *
 * The followings are the available columns in table '{{educators}}':
 * @property string $eduer_id
 * @property integer $user_id
 * @property string $eduer_name
 * @property string $eduer_gender
 * @property string $eduer_cover
 * @property string $eduer_tags
 * @property string $eduer_email
 * @property string $eduer_bio
 * @property integer $eduer_likes
 * @property integer $eduer_subscribes
 * @property integer $eduer_audio_counts
 * @property string $eduer_status
 * @property integer $dateline
 * @property string $eduer_province
 * @property string $eduer_city
 * @property string $eduer_area
 */
class Educators extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{educators}}';
	}
		
	public $province;
	public $city;
	public $area;
	//provience
	//city
	//area

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, eduer_likes, eduer_subscribes, eduer_audio_counts, dateline', 'numerical', 'integerOnly'=>true),
			array('eduer_name', 'length', 'max'=>50),
			array('eduer_gender, eduer_status', 'length', 'max'=>1),
			array('eduer_cover, eduer_tags, eduer_bio', 'length', 'max'=>255),
			array('eduer_email', 'length', 'max'=>200),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('eduer_id, user_id, eduer_name, eduer_gender, eduer_cover, eduer_tags, eduer_email, eduer_bio, eduer_likes, eduer_subscribes, eduer_audio_counts, eduer_status, dateline', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'profile' => array(self::BELONGS_TO, 'YumProfile', 'user_id'),
				'user' => array(self::BELONGS_TO, 'YumUser', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'eduer_id' => '教育家ID',
			'user_id' => '用户ID',
			'eduer_name' => '教育频道的名称',
			'eduer_gender' => '性别',
			'eduer_cover' => '头像',
			'eduer_tags' => '标签',
			'eduer_email' => '邮箱',
			'eduer_bio' => '简介',
			'eduer_likes' => '多少人喜欢你这个频道',
			'eduer_subscribes' => '教育节目订阅数',
			'eduer_audio_counts' => '音频数量',
			'eduer_province'=>'省份',
			'eduer_city'=>'城市',
			'eduer_area'=>'地区',
			'eduer_status' => '教育家状态',
			'dateline' => '认证时间',
		);
	}
	
	/**
	 * 获取教育家封面
	 */
	public function getAccessCover(){
		return Yii::app() -> qiniu -> getEduCover($this -> eduer_cover);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('eduer_id',$this->eduer_id,true);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('eduer_name',$this->eduer_name,true);
		$criteria->compare('eduer_gender',$this->eduer_gender,true);
		$criteria->compare('eduer_cover',$this->eduer_cover,true);
		if($this -> eduer_tags){
			$criteria->addSearchCondition('eduer_tags',$this->eduer_tags);
		}
		$criteria->compare('eduer_email',$this->eduer_email,true);
		$criteria->compare('eduer_bio',$this->eduer_bio,true);
		$criteria->compare('eduer_likes',$this->eduer_likes);
		$criteria->compare('eduer_subscribes',$this->eduer_subscribes);
		$criteria->compare('eduer_audio_counts',$this->eduer_audio_counts);
		$criteria->compare('eduer_status',$this->eduer_status,true);
		$criteria->compare('dateline',$this->dateline);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * (non-PHPdoc)
	 * @see CActiveRecord::afterSave()
	 */
	public function afterSave(){
		parent::afterSave();
		EducatorTags::model()->updateFrequency('',$this -> eduer_tags);
		$user_id = $this -> user_id;
		
		//更新昵称  教育家无法修改 更新用户类型
		return YumProfile::model()-> updateAll(array('nicke'=>$this -> eduer_name,'user_type'=>'2'),'user_id=:userid',array('userid'=>$user_id));
	}
	
	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Educators the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	/**
	 * (non-PHPdoc)
	 * @see CActiveRecord::beforeSave()
	 */
	public function beforeSave(){
		
		$tags = EducatorTags::string2array($this -> eduer_tags);
		
		$this -> dateline = time();
		return true;
	}
	
	
	/**
	 * 获取访问教育家个人主页的页面地址
	 */
	public function getAccessUrl(){
		return Yii::app()-> createUrl('/@'.$this -> eduer_name);
	}
	
	/**
	 * 获取订阅教育家的页面 
	 */
	public function getSubscribeUrl($nick = false){
		
		if($nick){
			return '/@'.$nick.'/subscribe';
		}
		
		$user_id = Yii::app() -> user -> id;
		$eduid  = $this -> user_id;
		if($this -> canSubscribe($eduid)){
			//can subscrible url
			return 1;
		}else{
			//can't subscrible url
			return 2;
		}
	}
	
	/**
	 * 取消对该教育家的订阅
	 * @param string $nick
	 */
	public function getUnSubscribeUrl($nick = false){
		if($nick){
			return '/@'.$nick.'/unsubscribe';
		}
		
		$user_id = Yii::app() -> user -> id;
		$eduid  = $this -> user_id;
		if($this -> canSubscribe($eduid)){
			//can subscrible url
			return 1;
		}else{
			//can't subscrible url
			return 2;
		}
	}
	
	/**
	 * 获取点赞的URL
	 * @param string $nick
	 */
	public function getLikeUrl($nick = false){
		if($nick){
			return '/@'.$nick.'/like';
		}
	}
	
	/**
	 * 取消赞
	 * @param string $nick
	 */
	public function getDislikeUrl($nick = false){
		if($nick){
			return '/@'.$nick.'/dislike';
		}
	}
	
	/**
	 * 查看用户是否可以订阅这个教育家
	 * @param unknown_type $eduid
	 * @return boolean
	 * 
	 */
	public function canSubscribe($eduid){
		$from = Yii::app() -> user -> id;
		$criteria = new CDbCriteria();
		$criteria -> compare('subscribe_from', $from);
		$criteria -> compare('subscribe_uid', $eduid);
		$exists = EducatorSubscribes::model()-> exists($criteria);
		return $exists?false:true;
	}
	
	/**
	 * 能否给赞
	 * @param int $eduid
	 * @return boolean
	 */
	public function canLike($eduid){
		$from = Yii::app() -> user -> id;
		$criteria = new CDbCriteria();
		$criteria -> compare('like_from', $from);
		$criteria -> compare('like_to', $eduid);
		$exists = EducatorLikes::model()-> exists($criteria);
		return $exists?false:true;
	}
	
	/**
	 * 订阅教育家
	 * @param integer $eduid
	 */
	public function setSubscribe($eduid){
		$from = Yii::app() -> user -> id;
		$EducatorSubscribes = new EducatorSubscribes();
		$EducatorSubscribes -> subscribe_id = NULL;
		$EducatorSubscribes -> subscribe_from = $from;
		$EducatorSubscribes -> subscribe_uid = $eduid;
		$EducatorSubscribes -> subscribe_dateline = time();
		return $EducatorSubscribes -> save();
	}
	
	/**
	 * 给教育家赞
	 * @param integer $eduid
	 */
	public function setLike($eduid){
		
		$from = Yii::app() -> user -> id;
		
		$educator_good = new EducatorLikes();
		$educator_good -> like_from = $from;
		$educator_good -> like_to = $eduid;
		$educator_good -> dateline = time();
		$educator_good -> save();
		
		$criteria=new CDbCriteria;
		$criteria->compare('user_id', $eduid);
		return $this->updateCounters(array('eduer_likes'=>1),$criteria);
		
	}
	
	/**
	 * 取消订阅
	 * @param integer $eduid
	 */
	public function setUnSubscribe($eduid){
		
		$from = Yii::app() -> user -> id;
		$criteria = new CDbCriteria();
		$criteria -> compare('subscribe_from', $from);
		$criteria -> compare('subscribe_uid', $eduid);
		
		$EducatorSubscribes = EducatorSubscribes::model() -> find($criteria);
		return $EducatorSubscribes -> delete();
		
	}
	
	/**
	 * 取消教育家的赞
	 * @param integer $eduid
	 */
	public function setDislike($eduid){
		$from = Yii::app() -> user -> id;
		
		$educator_good = EducatorLikes::model();
		$model = $educator_good -> findByAttributes(array('like_from'=>$from,'like_to'=>$eduid));
		$model -> delete();
		
		$criteria=new CDbCriteria;
		$criteria->compare('user_id', $eduid);
		return $this->updateCounters(array('eduer_likes'=>-1),$criteria);
	}

}
